package com.gz.mylife.app;


import org.apache.http.Header;

import com.gz.mylife.Define;
import com.gz.mylife.R;
import com.gz.mylife.im.ChatSession;
import com.gz.mylife.widget.LoadingCover;
import com.gz.mylife.widget.LoadingCover.OnLoadingCoverRefreshListener;
import com.zl.smartmall.library.ISmartMall;
import com.zl.smartmall.library.listener.MyAddressListener;
import com.zl.smartmall.library.po.MyAddress;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ContactTenement extends BaseActivity implements OnClickListener, OnLoadingCoverRefreshListener {

	private RelativeLayout call_phone;
	private TextView tv_phone;// 电话号码
	private TextView iv_device_repairs;// 设备保修
	private TextView repairlist;// 报修列表
	private MyAddress address;
	private LoadingCover mLoadingCover;
	private TextView iv_online_ask;//在线咨询

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_contact_tenement);
		setActionbarTitle("联系客服", false);
		mLoadingCover = (LoadingCover) findViewById(R.id.layout_loading_cover);
		mLoadingCover.setOnLoadingCoverRefreshListener(this);
		call_phone = (RelativeLayout) findViewById(R.id.call);
		call_phone.setOnClickListener(this);
		tv_phone = (TextView) findViewById(R.id.tv_phone);
		iv_device_repairs = (TextView) findViewById(R.id.iv_device_repairs);
		iv_device_repairs.setOnClickListener(this);
		repairlist = (TextView) findViewById(R.id.repairlist);
		repairlist.setOnClickListener(this);
		iv_online_ask = (TextView)findViewById(R.id.iv_online_ask);
		iv_online_ask.setOnClickListener(this);
		ISmartMall.getInstance().getMyAddress(addressListener());

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.contact_tenement, menu);
		return true;
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.call:// 点击联系客服到手机系统拨号盘
			String number = tv_phone.getText().toString();
			Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+ number));// 用intent启动拨打电话(只显示手机号码不直接拨打电话)
			// Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+number));//直接拨打电话
			startActivity(intent);
			break;
		case R.id.iv_device_repairs:// 点击设备报修进入选项页面
			if (address==null) {
				Toast.makeText(getApplicationContext(), "您还没有绑定房屋,暂时不能保修!", Toast.LENGTH_LONG).show();
			}else{
				Intent intent1 = new Intent(ContactTenement.this,ApplyEquimentActivity.class);
				intent1.putExtra("phone", address.getPhone());
				intent1.putExtra("name", address.getReceiver());
				intent1.putExtra("address", address.getAddressparam());
				intent1.putExtra("mallcomid", address.getMallcomid());
				startActivity(intent1);
			}
			break;
		case R.id.repairlist:// 点击报修列表进入我的申报列表
			Intent intent2 = new Intent(ContactTenement.this,MyEquipments.class);
			intent2.putExtra("address", address.getAddressparam());
			intent2.putExtra("mallcomid",address.getMallcomid());
			startActivity(intent2);
			break;
		case R.id.iv_online_ask:
			Intent intent3 = new Intent();
			intent3.setClass(getApplicationContext(), ChatSession.class);
			intent3.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			intent3.putExtra("fuid", address.getCustomerServices().get(0).getUid());
			intent3.putExtra("nickname", address.getCustomerServices().get(0).getNickname());
			intent3.putExtra("gender", address.getCustomerServices().get(0).getGender());
			intent3.putExtra("face", address.getCustomerServices().get(0).getFace());
			startActivity(intent3);
			break;
		default:
			break;
		}
	}

	private MyAddressListener addressListener() {
		return new MyAddressListener() {

			@Override
			public void onResponseSuccess(Header[] headers, MyAddress address) {
				// TODO Auto-generated method stub
				ContactTenement.this.address = address;
				tv_phone.setText(address.getCompanyphone());
				mLoadingCover.onRefreshSuccess();
			}

			@Override
			public void onNoNetWork() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed(Define.MSG_NO_NETWORK);
			}

			@Override
			public void onResponseFailed() {
				// TODO Auto-generated method stub
				mLoadingCover.onRefreshFailed();
			}

		};

	}

	@Override
	public void onLoadingCoverRefresh() {
		// TODO Auto-generated method stub
		
	}

}
